package com.git.smp.entity;

import java.util.ArrayList;
import java.util.List;

public class SysOrgTree {
    
	private List<SysOrgExtend> orgList = new ArrayList<>();
	
	public SysOrgTree(List<SysOrgExtend> orgList) {
		this.orgList = orgList;
	}
	
	/**
	  * 功能描述: 获取根节点
	  */
	 private List<SysOrgExtend> getRootNode() {
	     List<SysOrgExtend> rootOrgList = new ArrayList<>();
	     for (SysOrgExtend orgNode : orgList) {
	         if (orgNode.getParentId().equals("0")) {
	        	 rootOrgList.add(orgNode);
	         }
	     }
	     return rootOrgList;
	 }

	 /**
	  * 功能描述: 递归，根据根目录获取子目录
	  */
	 private SysOrgExtend buildChildTree(SysOrgExtend pNode) {
	     List<SysOrgExtend> childOrg = new ArrayList<SysOrgExtend>();
	     for (SysOrgExtend orgNode : orgList) {
	         if (orgNode.getParentId().equals(pNode.getSeqCate())) {
	        	 childOrg.add(buildChildTree(orgNode));
	         }
	     }
	     pNode.setChildren(childOrg);
	     return pNode;
	 }
 
	 public List<SysOrgExtend> buildTree() {
		 List<SysOrgExtend> rootNodes = getRootNode();

		 for (SysOrgExtend rootNode : rootNodes) {
			 buildChildTree(rootNode);
		}
	     
	     return rootNodes;
	 }
    
}